﻿using com.igetui.api.openservice;
using com.igetui.api.openservice.igetui;
using com.igetui.api.openservice.igetui.template;
using com.igetui.api.openservice.payload;
using Sc.Domain;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Service
{
    public class CommPushMessage
    {
        /// <summary>
        /// 应用ID
        /// </summary>
        private static string APPID = "FOg6MHZN7s6qedLtktIwS6";
        /// <summary>
        /// 个推APPKEY
        /// </summary>
        private static string APPKEY = "AStlfMVNkf8tV8DEkmhfx8";
        /// <summary>
        /// 个推APP对应编码
        /// </summary>
        private static string MASTERSECRET = "QHz79TWXhnABziIMKKcBS5";
        /// <summary>
        /// 个推第三方接口
        /// </summary>
        private static string HOST = "http://sdk.open.api.igexin.com/apiex.htm";

        /// <summary>
        /// 安卓推送
        /// </summary>
        /// <param name="ent">推送参数</param>
        public void PushMessageForAndroid(MessageParam ent)
        {
            try
            {
                IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
                ListMessage message = new ListMessage();
                NotificationTemplate template = new NotificationTemplate();
                template.AppId = APPID;
                template.AppKey = APPKEY;
                template.Title = ent.Title;
                template.Text = ent.Content;
                template.TransmissionType = "1";
                template.TransmissionContent = "{Id:" + ent.BusinessId + ",Perfix:'" + ent.Perfix + "'}";
                message.Data = template;
                List<Target> targetList = new List<Target>();
                foreach (var item in ent.UserAndroidToken)
                {
                    Target client = new Target();
                    client.appId = APPID;
                    client.clientId = item;
                    targetList.Add(client);
                }
                String contentId = push.getContentId(message);
                string tmp = push.pushMessageToList(contentId, targetList);
               
            }
            catch (Exception ex)
            {
            }
        }
        /// <summary>
        /// 消息推送(IOS模板)
        /// </summary>
        /// <param name="ent">推送参数</param>
        /// <returns></returns>
        public void PushMessageForIOS(MessageParam ent)
        {
            try
            {
                IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
                APNTemplate template = new APNTemplate();
                APNPayload apnpayload = new APNPayload();
                DictionaryAlertMsg alertMsg = new DictionaryAlertMsg();
                alertMsg.Body = ent.Content;
                alertMsg.Title = ent.Title;
                apnpayload.AlertMsg = alertMsg;
                apnpayload.Badge = 1;
                apnpayload.addCustomMsg("Id", ent.BusinessId);
                apnpayload.addCustomMsg("Perfix", ent.Perfix);
                template.setAPNInfo(apnpayload);
                ListMessage listmessage = new ListMessage();
                listmessage.Data = template;
                listmessage.IsOffline = true;
                listmessage.OfflineExpireTime = 1000 * 3600 * 12;
                string contentId = push.getAPNContentId(APPID, listmessage);
                string m = push.pushAPNMessageToList(APPID, contentId, ent.UserIosToken);
            }
            catch (Exception ex)
            {
            }
        }
    }
}
